package com.bw.lib_common.view

import android.os.Bundle
import android.util.Log
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.view.BaseActivity1
import com.bw.lib_common.vm.BaseViewModel3
import java.lang.reflect.ParameterizedType

abstract class BaseMvvmActivity1<VDB:ViewDataBinding,VM: BaseViewModel3>: BaseActivity1<VDB>() {

    private val type by lazy { (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments }

    //直接通过泛型拿到对应的ViewModel的class,来进行实例化
    protected val vm: VM by lazy {
        val clazz = type[1] as Class<VM>
        ViewModelProvider(this)[clazz]
    }

    open fun onError(t: Throwable) {
        ToastUtils.showLong(t.message)
        Log.e("TagA", "error:${t.message}")
        t.printStackTrace()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        vm.onFailed.observe(this, ::onError)
        super.onCreate(savedInstanceState)

    }



}